* set path

	global path "~/agriculture_temperature"
	
	use $path/data/merged_for_reg, clear

	xtset ctycode year

* merge in WBGT-based bins

	merge 1:1 ctycode year using $path/data/weather_idw_1985_2015_WBGT
	drop _merge
	
* compare distribution
	
	replace WBdd5bin30 = WBdd5bin30 + WBdd5bin35
	drop WBdd5bin35 
	
	egen WBdd5binPos = rowtotal(WBdd5bin*)
	gen WBdd5binNeg = 365 -  WBdd5binPos
	replace WBdd5binNeg = 0 if WBdd5binNeg < 0
	drop WBdd5binPos		
	
* drop ref bin
	drop WBdd5bin10
	
* results based on WBGT(Figure C2 and Figure C3)

	estimates clear 
	
	tempfile r1 r2 r3
	
	* all areas
	foreach t in 5 {
		foreach x in livestock_output {
			qui reghdfe ln_`x' WBdd`t'bin* ///
					c.pre##c.pre c.ssd##c.ssd c.prs##c.prs c.win##c.win ///
					i.pro#c.year##c.year ///
					if inrange(year,1995,2015) & inlist(region3,1,2), ///
					absorb(ctycode year) cluster(ctycode proXyear) 
			estimates store all_`x'_`t'C
		parmest , label saving(`"`r1'"',replace) format(p %8.2f) stars(0.1 0.05 0.01)  idstr("Aggregate")

	***north****		
		qui reghdfe ln_`x' WBdd`t'bin* ///
					c.pre##c.pre c.ssd##c.ssd c.prs##c.prs c.win##c.win ///
					i.pro#c.year##c.year ///
					if inrange(year,1995,2015) & inlist(region3,1), ///
					absorb(ctycode year) cluster(ctycode proXyear) 
			estimates store north_`x'_`t'C
		parmest , label saving(`"`r2'"',replace) format(p %8.2f) stars(0.1 0.05 0.01)  idstr("North")

	****south*****
			qui reghdfe ln_`x' WBdd`t'bin* ///
					c.pre##c.pre c.ssd##c.ssd c.prs##c.prs c.win##c.win ///
					i.pro#c.year##c.year ///
					if inrange(year,1995,2015) & inlist(region3,2), ///
					absorb(ctycode year) cluster(ctycode proXyear) 
			estimates store south_`x'_`t'C
		parmest , label saving(`"`r3'"',replace) format(p %8.2f) stars(0.1 0.05 0.01)  idstr("South")	
		
			preserve 
			use `"`r1'"',clear
			append using `"`r2'"' `"`r3'"' 
			gen product = "`x'"
			rename id  region
			save $temp/est_lvstkvalWB_`x',replace
			restore			
			
			}
		}
		
	esttab *output*5C, b(4) se(4) keep(WBdd*bin*) brackets star(* 0.1 ** 0.05 *** 0.01)		
	
* product-level results based on WBGT
	
	tempfile r2 r3

	estimates clear
	foreach x in total_meat_production pork_mutton_beef_prod poultry_rabbit_meat_prod slaughter_pig ///
		 milk_production  eggs_production {
	***north***
		qui reghdfe ln_`x' WBdd5bin* ///
			c.pre##c.pre c.ssd##c.ssd c.prs##c.prs c.win##c.win ///
			i.pro#c.year##c.year ///
			if inrange(year,1995,2015) & region3==1,  ///
			absorb(ctycode year) cluster(ctycode proXyear) 
			estimates store n_`x'
		parmest , label saving(`"`r2'"',replace) format(p %8.2f) stars(0.1 0.05 0.01)  idstr("North")

	***south***	
		qui reghdfe ln_`x' WBdd5bin* ///
			c.pre##c.pre c.ssd##c.ssd c.prs##c.prs c.win##c.win ///
			i.pro#c.year##c.year ///
			if inrange(year,1995,2015) & region3==2,  ///
			absorb(ctycode year) cluster(ctycode proXyear) 
			estimates store s_`x'
		parmest , label saving(`"`r3'"',replace) format(p %8.2f) stars(0.1 0.05 0.01)  idstr("South")	

			preserve 
			use `"`r2'"',clear
			append using `"`r3'"' 
			gen product = "`x'"
			rename id  region
			save $temp/est_lvstkprdWB_`x',replace
			restore			
					
			}	
	
	esttab n_*, b(4) se(4) keep(WBdd*bin*) brackets	
	esttab s_*, b(4) se(4) keep(WBdd*bin*) brackets	
	
	
	use $temp/est_lvstkvalWB_livestock_output,clear
	foreach x in total_meat_production pork_mutton_beef_prod poultry_rabbit_meat_prod slaughter_pig ///
		 milk_production  eggs_production {
		append using $temp/est_lvstkprdWB_`x'
	}
	
	// keep temp variables only
	keep if strmatch(parm,"*dd5bin*")
	
	//level varibables
	gen level = 2.5 if strmatch(parm, "WBdd5bin0")
	replace level = 7.5 if strmatch(parm, "WBdd5bin5")
	replace level = -2.5 if strmatch(parm, "WBdd5binNeg")
	*replace level = 12.5 if strmatch(parm, "WBdd5bin10")
	replace level = 17.5 if strmatch(parm, "WBdd5bin15")
	replace level = 22.5 if strmatch(parm, "WBdd5bin20")
	replace level = 27.5 if strmatch(parm, "WBdd5bin25")
	replace level = 32.5 if strmatch(parm, "WBdd5bin30")
	replace level = 37.5 if strmatch(parm, "WBdd5bin35")

	
	// product name 
	replace product = "Total Meat" if strmatch(product,"total_meat_production")
	replace product = "Pork, Mutton, Beef" if strmatch(product,"pork_mutton_beef_prod")
	replace product = "Poultry Meat" if strmatch(product,"poultry_rabbit_meat_prod")
	replace product = "Pig Slaughter" if strmatch(product,"slaughter_pig")
	replace product = "Milk" if strmatch(product,"milk_production")
	replace product = "Egg" if strmatch(product,"eggs_production")
		
	rename estimate coef 
	rename stderr se 
	rename min95 ci_l
	rename max95 ci_h
	
	// add zero-value lines
	bysort region product : gen last = _n == _N
	expand 2 if last
	sort region product parm
	replace level = 12.5 if parm==parm[_n-1]
	replace coef = 0 if parm==parm[_n-1]
	replace se = 0 if parm==parm[_n-1]
	replace ci_h = 0 if parm==parm[_n-1]
	replace ci_l = 0 if parm==parm[_n-1]			
	
	save $visual/visual_livestockWB,replace	
	
	// delete dta data 
	cd $temp/
	fs *.dta
	foreach f in `r(files)'{
	   erase "`f'"
	}	
